package kr.co.redbean.frame
{
	import com.greensock.TimelineMax;
	import com.greensock.TweenLite;
	import com.greensock.easing.Expo;
	import com.greensock.easing.Quint;
	import com.greensock.events.TweenEvent;
	
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	
	import kr.co.redbean.events.ForegroundLoaderEvent;
	import kr.co.redbean.store.IObjectInfo;
	
	public class MenuFrame extends MainFrame implements IFrame
	{
		private var frameLine:Shape;
		private var displayMask:Sprite;
		private var progressBar:Sprite;
		private var _tween:TimelineMax;
		private var _currentFocus:String;
		private var _currentObj:DisplayObject;
		private var _lastObj:DisplayObject;
		
		public function MenuFrame()
		{
			super();

			frameLine = new Shape;
			displayMask = new Sprite;
			displayMask.mouseEnabled = false;
			progressBar = new Sprite;
			progressBar.mouseEnabled = false;
			_tween = new TimelineMax({paused:true});
			
			addChild(frameLine);
			addChild(displayMask);
			addChild(progressBar);

			addEventListener(Event.ADDED_TO_STAGE, setPlacement);
		}
		
		public function doSetupMotion(objectInfo:IObjectInfo):void {
			_tween.appendMultiple( [
				new TweenLite(displayMask, .5, { height: 10, y: FRAME_HEIGHT/2 - 5, ease: Quint.easeIn }),
				new TweenLite(frameLine, .5, { height: 11, y: FRAME_HEIGHT/2 - 6, ease: Quint.easeIn })
				]
			);
			_tween.play();
			
			progressBar.alpha = 1;
			progressBar.x = 0;
			progressBar.y = FRAME_HEIGHT/2 - 5;
			progressBar.graphics.clear();
			progressBar.graphics.beginFill(0x2b1a1b);
			progressBar.graphics.drawRect(0, 0, 1, 10);
			progressBar.graphics.endFill();
			progressBar.width = 0;
		}
		
		public function doProgressMotion(bytesLoaded:uint, bytesTotal:uint):void {
//			_tween.append(new TweenLite(progressBar, 0.2, {width: bytesLoaded/(bytesTotal*posS.right) }));
			progressBar.width = bytesLoaded/(bytesTotal*posS.right);
		}
		
		public function doCompleteMotion(displayObject:DisplayObject):void {
			_tween.append(new TweenLite(progressBar, 0.2, { width: posS.right, onStart: hnTweenStart }));
			_tween.append(new TweenLite(displayObject, 0.1, { alpha: 1.0, ease: Expo.easeOut }));
			_tween.append(new TweenLite(progressBar, 0.3, { alpha: 0 } ));
			_tween.appendMultiple( [
				new TweenLite(displayMask, .5, { height: FRAME_HEIGHT, y: 0, ease: Quint.easeOut}),
				new TweenLite(frameLine, .5, {height: FRAME_HEIGHT+2, y: 0, ease: Quint.easeOut})
				]
			);
		}
		
		public function setPlacement($e:Event=null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, setPlacement);
			
			setLinePlacement();
			setDisplayPlacement();
			setPosition();
		}
		
		private function setPosition():void {
			x = 0;
			y = int(posF.top);
			
			var _obj:DisplayObject;
			for(var i:uint=0, j:uint=numChildren; i<j; i++) { 
				_obj = getChildAt(i);
				if(_obj.toString() === '[object Shape]' || _obj.toString() === '[object Sprite]') continue;
				_obj.x = posS.right/2 - _obj.width/2;
				_obj.y = 0;
			}
		}
		private function setDisplayPlacement():void {
			displayMask.graphics.clear();
			displayMask.graphics.beginFill(0xf7f7f7);
			displayMask.graphics.drawRect(0, 0, posS.right, FRAME_HEIGHT);
			displayMask.graphics.endFill();
		}
		private function setLinePlacement():void {
			frameLine.graphics.clear();
			frameLine.graphics.lineStyle(1, 0x2b1a1b);
			frameLine.graphics.moveTo(0, -1);
			frameLine.graphics.lineTo(posS.right, -1);
			frameLine.graphics.moveTo(0, FRAME_HEIGHT);
			frameLine.graphics.lineTo(posS.right, FRAME_HEIGHT);
		}
		
		private function hnTweenStart():void {
			if(_lastObj !== null) {
				removeChild(_lastObj);
			}
			_currentObj.mask = displayMask;
			_currentObj.alpha = 0;
			addChildAt(_currentObj,0);
			setPosition();
		}
		
		public function addObject(obj:DisplayObject):void {
			_lastObj = _currentObj;
			_currentObj = obj;
		}
		
		public function get currentFocus():String
		{
			return _currentFocus;
		}

		public function set currentFocus(value:String):void
		{
			_currentFocus = value;
		}

	}
}